package service.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import domain.vo.queryObject;

import module.Customers;
import module.EmployeeCustomer;
import module.HousesCustomers;
import module.Phones;
import service.customersService;

public class CustomersServiceImpl extends customersService {
	private Logger logger = Logger.getLogger(CusLogServiceImpl.class.getName());

	/*
	 * (non-Javadoc)
	 * 
	 * @see service.basicService#countByQueryObject(domain.vo.queryObject)
	 */
	public int countByQueryObject(queryObject queryObject) throws SQLException {
		// TODO Auto-generated method stub
		return customerDAO.countByQueryObject(queryObject);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see service.basicService#getObjectByQueryObject(domain.vo.queryObject)
	 */
	public List<Customers> getObjectByQueryObject(queryObject queryObject)
			throws SQLException {
		return customerDAO.getObjectByQueryObject(queryObject);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see service.basicService#deleteT(java.lang.Object)
	 */
	public void deleteT(Customers t) throws SQLException {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see service.basicService#getAll()
	 */
	public List<Customers> getAll() throws SQLException {
		// TODO Auto-generated method stub
		return customerDAO.getAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see service.basicService#getById(java.lang.String)
	 */
	public Customers getById(String id) throws SQLException {
		return customerDAO.getById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see service.basicService#insertObject(java.lang.Object)
	 */
	public void insertObject(Customers t) throws SQLException {
		try {
			sqlMapClient.startTransaction();
			customerDAO.insertT(t);
			sqlMapClient.startBatch();
			logger.debug("保存电话对象");
			for (Phones p : t.getPhones()) {
				p.setCus_id(t.getCus_id());
				phonesDAO.insertT(p);
			}
			logger.debug("保存房屋与客户对象");
			for (HousesCustomers hc : t.getHces()) {
				hc.setCusid(t.getCus_id());
				housesCustomersDAO.insertT(hc);
			}
			logger.debug("保存客户与员工对象");
			for(EmployeeCustomer ec : t.getEmployeeCustomers()) {
				ec.setCus_id(t.getCus_id());
				employeeCustomerDAO.insertT(ec);
			}
			sqlMapClient.executeBatch();
			sqlMapClient.commitTransaction();
		} finally {
			sqlMapClient.endTransaction();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see service.basicService#updateObject(java.lang.Object)
	 */
	public void updateObject(Customers t) throws SQLException {
		// customerDAO.updateT(t);
		try {
			sqlMapClient.startTransaction();
	
			sqlMapClient.startBatch();
			if (t.getPhones().size() > 0) {
				for (Phones p : t.getPhones()) {
					p.setCus_id(t.getCus_id());
					phonesDAO.insertT(p);
				}
			} else if(  t.getEmployeeCustomers().size() > 0) {
				customerDAO.updateT(t);
				logger.debug("保存客户与员工对象");
				for(EmployeeCustomer ec : t.getEmployeeCustomers()) {
					ec.setCus_id(t.getCus_id());
					employeeCustomerDAO.insertT(ec);
				}
			}else {
				customerDAO.updateT(t);
			}
			sqlMapClient.executeBatch();
			sqlMapClient.commitTransaction();
		} finally {
			sqlMapClient.endTransaction();
		}

	}

}
